What is unist-util-is?
The unist-util-is npm package is a utility library for working with Unist nodes. Unist (Universal Syntax Tree) is part of the unified ecosystem, which provides a way to work with syntax trees for content such as Markdown, HTML, or plain text. unist-util-is specifically helps in testing and filtering nodes in these trees based on certain conditions.
What are unist-util-is's main functionalities?
Test nodes with a condition
This feature allows you to test if a node matches a specific type or condition. In the example, `is` is used to check if the root node is a 'leaf' (which it isn't), and then if the first child of the root is a 'leaf' (which it is).
const is = require('unist-util-is');
const u = require('unist-builder');
const tree = u('root', [
u('leaf', 'first leaf'),
u('node', [u('leaf', 'nested leaf')])
]);
const test = is(tree, 'leaf'); // false
const testLeaf = is(tree.children[0], 'leaf'); // true
Filter nodes by type
This feature demonstrates how to filter nodes by type using `is` in combination with `unist-util-select`. It selects all 'leaf' nodes from the tree and filters them to ensure they are of type 'leaf'.
const is = require('unist-util-is');
const u = require('unist-builder');
const select = require('unist-util-select');
const tree = u('root', [
u('leaf', 'first leaf'),
u('node', [u('leaf', 'nested leaf')])
]);
const leaves = select.selectAll('leaf', tree).filter(node => is(node, 'leaf'));
Other packages similar to unist-util-is
unist-util-visit
This package is similar to unist-util-is in that it is used to work with Unist nodes. However, unist-util-visit focuses on visiting nodes within a tree, optionally filtering nodes, and applying a function to each node. It differs from unist-util-is by providing traversal capabilities rather than just testing or filtering.
unist-util-select
unist-util-select is used to select nodes from a Unist tree using CSS-like selectors. It complements unist-util-is by providing a way to retrieve nodes based on complex queries, whereas unist-util-is is more focused on testing nodes against specified conditions.
unist-util-is
unist utility to check if a node passes a test.
Install
npm:
npm install unist-util-is
Use
var is = require('unist-util-is')
var node = {type: 'strong'}
var parent = {type: 'paragraph', children: [node]}
function test(node, n) {
return n === 5
}
is()
is({children: []})
is(node)
is(node, 'strong')
is(node, 'emphasis')
is(node, node)
is(parent, {type: 'paragraph'})
is(parent, {type: 'strong'})
is(node, test)
is(node, test, 4, parent)
is(node, test, 5, parent)
API
is(node[, test[, index, parent[, context]]])
Parameters
node
(Node
) — Node to check.test
(Function
, string
, Object
, or Array.<Test>
, optional)
— When nullish, checks if node
is a Node
.
When string
, works like passing node => node.type === test
.
When array
, checks if any one of the subtests pass.
When object
, checks that all keys in test
are in node
,
and that they have strictly equal valuesindex
(number
, optional) — Index of node
in parent
parent
(Node
, optional) — Parent of node
context
(*
, optional) — Context object to invoke test
with
Returns
boolean
— Whether test
passed and node
is a Node
(object with
type
set to a non-empty string
).
function test(node[, index, parent])
Parameters
Context
*
— The to is
given context
.
Returns
boolean?
— Whether node
matches.
is.convert(test)
Create a test function from test
, that can later be called with a node
,
index
, and parent
.
Useful if you’re going to test many nodes, for example when creating a utility
where something else passes an is-compatible test.
The created function is slightly faster because it expects valid input only.
Therefore, passing invalid input, yields unexpected results.
Can also be accessed with require('unist-util-is/convert')
.
For example:
var u = require('unist-builder')
var convert = require('unist-util-is/convert')
var test = convert('leaf')
var tree = u('tree', [
u('node', [u('leaf', '1')]),
u('leaf', '2'),
u('node', [u('leaf', '3'), u('leaf', '4')]),
u('leaf', '5')
])
var leafs = tree.children.filter((child, index) => test(child, index, tree))
console.log(leafs)
Yields:
[{type: 'leaf', value: '2'}, {type: 'leaf', value: '5'}]
Related
Contribute
See contributing.md
in syntax-tree/.github
for ways to get
started.
See support.md
for ways to get help.
This project has a code of conduct.
By interacting with this repository, organization, or community you agree to
abide by its terms.
License
MIT © Titus Wormer